﻿//Copyright (c) 2010 William Liu, Enoch Ye
//William Liu <william.lyz@gmail.com>
//Enoch Ye <moonlight.enoch@gmail.com>

//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at

//       http://www.apache.org/licenses/LICENSE-2.0

//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL;
using GBG_DBM.Support.DAL.DB;
using GBG_DBM.Support.DAL.Entities;

namespace GBG_DBM.UI
{
    public partial class Supplier_RM_View_UI : Form
    {
        private DBAccessory _DBAccessory = new DBAccessory();
		private DataTable _CurrentIngredients;

		public DataTable CurrentIngredients
		{
			get
			{
				if (_CurrentIngredients == null) 
				{
					setCurrentIngredients();
				} 
				return _CurrentIngredients; }
			set { _CurrentIngredients = value; }
		}
		private Ingredient _CurrentIngredient;

		public Ingredient CurrentIngredient
		{
			get { return _CurrentIngredient; }
			set { _CurrentIngredient = value; }
		}
		private DataTable _CurrenyAndFreight;

		public DataTable CurrenyAndFreight
		{
			get
			{
				if (_CurrenyAndFreight == null) 
				{
					setCurrentFreightAndCurrency();
				} 
				return _CurrenyAndFreight; }
			set { _CurrenyAndFreight = value; }
		}
        public Supplier_RM_View_UI()
        {
            InitializeComponent();
        }
        public Supplier_RM_View_UI(Ingredient _Ingredient,string Cur,string EstimatePrice,string Fre,string EstimateLoss,string Pack,string DateTo,string Dateform) 
        {
            InitializeComponent();
			this.RetrieveData(_Ingredient, Cur,EstimatePrice, Fre, EstimateLoss, Pack, DateTo, Dateform);
             
        }
        private void RetrieveData(Ingredient _Ingredient,string Cur,string EstimatePrice,string Fre,string EstimateLoss,string Pack,string DateTo,string Datefrom) 
        {
			_CurrentIngredients=null;
			SetFromControlAndLoadData(CurrentIngredients,_Ingredient,Cur,EstimatePrice,Fre,EstimateLoss,Pack,DateTo,Datefrom);
        }
		private void setCurrentIngredients() 
		{
			_CurrentIngredients = new DataTable("Ingredient");
			_CurrentIngredients.Columns.Add("Code");
			_CurrentIngredients.Columns.Add("Supplier_Code");
			_CurrentIngredients.Columns.Add("Supplier Name");
			_CurrentIngredients.Columns.Add("INCINAME");
			_CurrentIngredients.Columns.Add("TradeName");
			_CurrentIngredients.Columns.Add("KeyProperty");
			_CurrentIngredients.Columns.Add("LocalPrice");
			_CurrentIngredients.Columns.Add("Category");
		}
		private void setCurrentFreightAndCurrency() 
		{
			_CurrenyAndFreight = new DataTable("CurrencyAndFreight");
			_CurrenyAndFreight.Columns.Add("Currency");
			_CurrenyAndFreight.Columns.Add("Local Price");
			_CurrenyAndFreight.Columns.Add("Estimated Price");
            _CurrenyAndFreight.Columns.Add("Freight Type");
            _CurrenyAndFreight.Columns.Add("Estimate Cost");
            _CurrenyAndFreight.Columns.Add("Estimated Loss");
			_CurrenyAndFreight.Columns.Add("Pack Size");
			_CurrenyAndFreight.Columns.Add("Date From");
			_CurrenyAndFreight.Columns.Add("Date To");
		}
		private void SetDataGridFormat() 
		{
			this.dgvIngredientSupplier.Columns["Code"].HeaderText = "Ingredient Code";
			this.dgvIngredientSupplier.Columns["Supplier_Name"].HeaderText = "Supplier Name";
			this.dgvIngredientSupplier.Columns["Supplier_Code"].HeaderText = "Supplier Code";
			this.dgvIngredientSupplier.Columns["INCIName"].HeaderText = "INCI Name";
			this.dgvIngredientSupplier.Columns["TradeName"].HeaderText = "Trade Name";
			this.dgvIngredientSupplier.Columns["KeyProperty"].HeaderText = "Key Property";
			this.dgvIngredientSupplier.Columns["LocalPrice"].HeaderText = "Local Price";
			this.dgvIngredientSupplier.Columns["Category"].HeaderText = "Category";
			this.dgvCurrencyFreight.Columns["Currency"].HeaderText = "Currency";
			this.dgvCurrencyFreight.Columns["Local Price"].HeaderText = "Currency Price";
			this.dgvCurrencyFreight.Columns["Estimated Price"].HeaderText = "Estimated Price";
			this.dgvCurrencyFreight.Columns["Estimated Loss"].HeaderText = "Estimated Loss";
			this.dgvCurrencyFreight.Columns["Freight Type"].HeaderText = "Freight Type";
			this.dgvCurrencyFreight.Columns["Pack Size"].HeaderText = "Pack Size";
			this.dgvCurrencyFreight.Columns["Date From"].HeaderText = "Date From";
			this.dgvCurrencyFreight.Columns["Date To"].HeaderText = "Date To";
		}
        private DataTable AddRowToIngredientSupplierGrid(DataTable CurrentIngredients, Ingredient _Ingredient) 
		{
            if (CurrentIngredients != null)
            {
                CurrentIngredients.Rows.Add(_Ingredient.IngredientCode,  _Ingredient.SupplierCode, _Ingredient.SupplierName,_Ingredient.SupplierCountry +
                _Ingredient.INCIName, _Ingredient.TradeName, _Ingredient.KeyProperties, _Ingredient.LocalPrice, _Ingredient.Category);
                return _CurrentIngredients;
            }
            else 
            {
                return null;
            }
		
		}
		private void AddRowToCurrencyAndFreightGrid(DataTable CurrencyAndFreight,DataRowView drv) 
		{
			if(CurrencyAndFreight!=null)
			{

			}else{}
		}
        private void SetFromControlAndLoadData(DataTable CurrentIngredients, Ingredient _Ingredient, string Currency, string estimateprice, string Freight, string EstimateLoss, string Pack, string DateTo, string Dateform) 
		{
          
            this.dgvIngredientSupplier.DataSource = AddRowToIngredientSupplierGrid(CurrentIngredients, _Ingredient);
            this.dgvCurrencyFreight.DataSource = getCurrencyAndFreight(Currency, estimateprice, Freight, EstimateLoss, Pack, DateTo, Dateform);
			this.dgvCurrencyFreight.Visible = true;
			this.dgvCurrencyFreight.ReadOnly = true;
			this.dgvIngredientSupplier.Visible = true;
			this.dgvIngredientSupplier.ReadOnly = true;
		}
		private DataTable getCurrencyAndFreight(string Currency,string estimatedprice,string Freight,string EstimateLoss,string Pack,string DateTo,string Dateform)
		{
			CurrencyDB _Currency = new CurrencyDB();
			FreightDB _Freight = new FreightDB();
			DataTable Cur = _Currency.getCurrency(Currency);
			DataTable Fre = _Freight.getFreight(Freight);
            float est = Convert.ToSingle(estimatedprice);
            int fre = Convert.ToInt32(Fre.Rows[0].ItemArray[1]);
            float estimateloss = Convert.ToSingle(EstimateLoss);
            lblTotalCost.Text = Convert.ToString((est + fre) * (1 + estimateloss));
            float TotalCost = Convert.ToSingle(lblTotalCost.Text);
            float p = Convert.ToSingle(Pack);
            lblTotalCostofShipment.Text = Convert.ToString(TotalCost * p);

            CurrenyAndFreight = null;
           
                //CurrenyAndFreight.Rows.Add();
                
            
            //for (int a = 0; a < Fre.Rows.Count; a++)
            //{
            //    CurrenyAndFreight.Rows.Add(Cur.Rows[a].ToString());
            //}   
			CurrenyAndFreight.Rows.Add(Cur.Rows[0].ItemArray[0], Cur.Rows[0].ItemArray[1],estimatedprice,Fre.Rows[0].ItemArray[0],Fre.Rows[0].ItemArray[1],EstimateLoss, Pack, Dateform, DateTo);
			return _CurrenyAndFreight;
		}
		private void Caculate() 
		{
			//Estimate Cost+Estimate Freight Cost*(1+Estimated Loss)
		}

		private void Cancel_Supplier_RW_UI_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void OK_Supplier_RM_View_UI_Click(object sender, EventArgs e)
		{
			
            Supplier_RM Supplier_RM=new Supplier_RM();
            Supplier_RM.Code = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[0]);
            Supplier_RM.SupplierCode = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[1]);
            Supplier_RM.SupplierName = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[2]);
            Supplier_RM.INCIName = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[3]);
            Supplier_RM.TradeName = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[4]);
            Supplier_RM.KeyProperty = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[5]);
            Supplier_RM.LocalPrice = Convert.ToSingle(_CurrentIngredients.Rows[0].ItemArray[6]);
            Supplier_RM.Category = Convert.ToString(_CurrentIngredients.Rows[0].ItemArray[7]);
            Supplier_RM.CurrencyName = Convert.ToString(_CurrenyAndFreight.Rows[0].ItemArray[0]);
            Supplier_RM.LocalPrice = Convert.ToSingle(_CurrenyAndFreight.Rows[0].ItemArray[1]);
            Supplier_RM.EstimatePrice = Convert.ToSingle(_CurrenyAndFreight.Rows[0].ItemArray[2]);
            Supplier_RM.FreightType = Convert.ToString(_CurrenyAndFreight.Rows[0].ItemArray[3]);
            Supplier_RM.EstimateCost = Convert.ToSingle(_CurrenyAndFreight.Rows[0].ItemArray[4]);
            Supplier_RM.EstimatedLoss = Convert.ToSingle(_CurrenyAndFreight.Rows[0].ItemArray[5]);
            Supplier_RM.Packsize = Convert.ToSingle(_CurrenyAndFreight.Rows[0].ItemArray[6]);
            Supplier_RM.EstimateDateFrom = Convert.ToString(_CurrenyAndFreight.Rows[0].ItemArray[7]);
            Supplier_RM.EstimateDateTo = Convert.ToString(_CurrenyAndFreight.Rows[0].ItemArray[8]);
            Supplier_RM.TotalCost = Convert.ToSingle(lblTotalCost.Text);
            Supplier_RM.TotalCostofShipment = Convert.ToSingle(lblTotalCostofShipment.Text);
            string t = _DBAccessory.getSupplierCode(Supplier_RM.Code);
            
            if (t == null)
            {
                _DBAccessory.addSupplierRM(Supplier_RM);
            }
            else 
            {
                _DBAccessory.setSupplierRM(Supplier_RM);

            } 
		}

		private void btnBack_Click(object sender, EventArgs e)
		{
            if (MessageBox.Show("All the Information will be deleted permanently, Are you sure?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.OK)
            {

            }
            else
            {
                this.Close();
            }
		}

    }
}
